var request = new XMLHttpRequest();
var hd;
var _data;

function hello(){
	if(request.readyState == 4 && request.status == 200){
		var test = request.responseText;
		var json = eval('(' + test+ ')');
		hd = json.head;
		console.log(hd);
		_data = json.content;
		console.log(_data);
		tabClose();
		tabCloseEven();
		clearHeadMenu('headmenu','li');
		setHeadMenu('headmenu',hd);
		$('#headmenu a').click(function(){
			$('#headmenu a').removeClass('active');
			$(this).addClass('active');
			var data = _data[$(this).attr('name')];
			clearNav();
			addNav(data);
			InitLeftMenu();
		});
		
		$('#lead').accordion({
			animate:false
		});
		var firstMenueName = $('#headmenu a').attr('name');
		console.log(_data[firstMenueName]);
		addNav(_data[firstMenueName]);
		InitLeftMenu();
	}
}
$(function(){
	//发送请求
	request.onreadystatechange = hello;
	request.open("POST","Init",true);
	request.send(null);
});

function setHeadMenu(id,data){
	var objId = "#" + id;
	var json = data;
	for(var i =0;i<json.length;i++){
		var liContent = "";
		liContent += '<li>';
		liContent += '<a name="'+ json[i].name+ '" title="' + json[i].title + '" >' + json[i].text + '</a>'; 
		liContent += '</li>';
		$(objId).append(liContent);
	}	
}

function clearHeadMenu(id,objName){
	var objId = "#" + id;
	$(objId + " "+ objName).remove();
}

function createFrame(url){
	var s='<iframe scrolling="auto" frameborder="0" src="' + url + '" style="width:100%;height:100%"><iframe>';
	return s;
}

function addNav(data){
	console.log(data.length);
	$.each(data,function(i,sm){
		var menulist ="";
		menulist +='<ul>';
		$.each(sm.menuList,function(j,o){
			menulist += '<li><div><a ref="' + o.id + '" href="#" rel="'
			+ o.url + '" ><span class="icon ' + o.icon
			+ '" >&nbsp;</span><span class="nav">' + o.name
			+ '</span></a></div></li> ';
		});
		menulist +='</ul>';
		$('#lead').accordion('add',{
			title :sm.name,
			content : menulist,
			iconCls : 'icon ' + sm.icon 
		});
	});
	var pp = $('#lead').accordion('panels');
	var t = pp[0].panel('options').title;
	$('#lead').accordion('select',t);
}

function clearNav(){
	var pp = $('#lead').accordion('panels');
	$.each(pp,function(i,n){
		if(n){
			var t= n.panel('options').title;
			$('#lead').accordion('remove',t);
		}
	});
	pp = $('#lead').accordion('getSelected');
	if(pp){
		var title = pp.panel('options').title;
		$('#lead').accordion('remove',title);
	}
}

function addTab(subtitle,url){
	if(!$('#tabs').tabs('exists',subtitle)){
		$('#tabs').tabs('add',{
			title:subtitle,
			content : createFrame(url),
			closable :true,
		});
	}
	else {
		$('#tabs').tabs('select',subtitle);
		$('#mm-tabuodate').click();
	}
	tabClose();
}

function tabClose(){
	$(".tabs-inner").dblclick(function(){
		var subtitle = $(this).children(".tabs-closable").text();
		if(subtitle != "欢迎使用"){
			$('#tabs').tabs('close',subtitle);
		}
	});
	
	$(".tabs-inner").bind("contextmenu",function(e){
		$('#mm').menu('show',{
			left:e.pageX,
			top:e.pageY
		});
		var subtitle = $(this).children(".tabs-closable").text();
		$('#mm').data("currtab",subtitle);
		$('#tabs').tabs('select',subtitle);
		return false;
	});
}

function tabCloseEven(){
	$('#mm-tabupdate').click(function() {
		var currTab = $('#tabs').tabs('getSelected');
		var url = $(currTab.panel('options').content).attr('src');
		$('#tabs').tabs('update', {
			tab : currTab,
			options : {
				content : createFrame(url)
			}
		});
	});
	
	$('#mm-tabcloseall').click(function(){
		var pp = $('#tabs').tabs('tabs');
		for(var i=pp.length - 1;i>=0;i--){
			var t =pp[i].panel('options').title;
			if(t != "欢迎使用"){
				$('#tabs').tabs('close',t);
			}
		}
	});
	
	$('#mm-tabclose').click(function(){
		var currtab = $('#mm').data("currtab");
		$('#tabs').tabs('close',currtab);
	});
	
	$('#mm-tabcloseother').click(function(){
		var currtab_title=$('#mm').data("currtab");
		var pp = $('#tabs').tabs('tabs');
		for(var i=pp.length - 1 ;i>= 0 ;i--){
			var t =pp[i].panel('options').title;
			if(t != "欢迎使用" && t != currtab_title){
				$('#tabs').tabs('close',t);
			}
		}
	});
}

function InitLeftMenu(){
	hoverMenuItem();
	
	$('#lead li a').on('click',function(){
		var tabTitle = $(this).children('.nav').text();
		var url = $(this).attr("rel");
		addTab(tabTitle,url);
		$('#lead li div').removeClass("selected");
		$(this).parent().addClass("selected");
	});
}

function hoverMenuItem(){
	$(".easyui-accordion").find('a').hover(function(){
		$(this).parent().addClass("hover");}
	,function(){
			$(this).parent().removeClass("hover");
		}
	);
}